/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-10
*/
package com.rzico.basics.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Company;
import com.rzico.basics.entity.CompanyEmployee;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.model.CompanyEmployeeVo;
import com.rzico.basics.service.CompanyEmployeeService;
import com.rzico.basics.service.CompanyService;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 企业员工控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-05-10
 */
@Api(description = "企业员工接口")
@RestController
@RequestMapping("/admin/companyEmployee")
public class CompanyEmployeeAdminController extends BaseController {

    @Autowired
    private CompanyEmployeeService companyEmployeeService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private CompanyService companyService;

    /**
     * 分页查询企业员工
     *
     * @return
    */
    @ApiOperation("分页查询企业员工")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "客户名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "联系电话", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "vip", value = "用户级别", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "companyId", value = "企业id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "Integer", paramType = "query")
    })
    public CommResult<CompanyEmployeeVo> list(String startDate, String endDate, String name, String mobile, String phone, Integer vip, Integer status, Long companyId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (StringUtils.isNotEmpty(startDate)){
            params.put("startDate", startDate);
        }
        if (StringUtils.isNotEmpty(endDate)){
            params.put("endDate", endDate);
        }
        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }

        if (name!=null) {
            params.put("name",name);
        }
        if (mobile!=null) {
            params.put("phone",mobile);
        }
        if (vip!=null) {
            params.put("vip",vip);
        }
        if (null != companyId) {
            params.put("companyId", companyId);
        }
        if (null != status) {
            params.put("status", status);
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<CompanyEmployeeVo> list = companyEmployeeService.selectCompanyEmployeeVoList(params);
        PageResult<CompanyEmployeeVo> pageResult = new PageResult<CompanyEmployeeVo>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条企业员工
     *
     * @return
    */
    @ApiOperation("查询单条企业员工")
    @GetMapping("/find/{memberId}")
    public CommResult<CompanyEmployee> find(@PathVariable Long memberId, Long companyId) {
        Enterprise enterprise = enterpriseService.getCurrent();
        if (null == enterprise) {
            return CommResult.error("没有开通企业");
        }
        Map<String,Object> params = new HashMap<>();
        params.put("memberId",memberId);
        params.put("companyId",companyId);
        params.put("enterpriseId", enterprise.getId());
        CompanyEmployeeVo result = companyEmployeeService.findById(params);

        Map<String,Object> data = new HashMap<>();
        data.put("companyEmployeeVo",result);
        return CommResult.success(data);

    }

    /**
     * 批量删除企业员工
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除企业员工", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除企业员工,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<CompanyEmployee> del(@PathVariable String ids) {

        companyEmployeeService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新企业员工
     *
     * @param companyEmployeeVo
     * @return
     */
    @Log(desc = "更新企业员工", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新企业员工")
    @PostMapping("/update")
    public CommResult<CompanyEmployee> update(@RequestBody CompanyEmployeeVo companyEmployeeVo) {
        Enterprise enterprise = enterpriseService.getCurrent();
        if (null == enterprise) {
            return CommResult.error("没有开通企业");
        }

        //修改员工信息
        if (null != companyEmployeeVo.getStatus() && 1 == companyEmployeeVo.getStatus()) {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("memberId", companyEmployeeVo.getMemberId());
            params.put("status", 1);
            params.put("enterpriseId", enterprise.getId());
            List<CompanyEmployee> employeeList = companyEmployeeService.selectList(params);
            for (CompanyEmployee companyEmployee:employeeList) {
                if (!companyEmployee.getCompanyId().equals(companyEmployeeVo.getCompanyId())) {
                    Company company = companyService.selectByPrimaryKey(companyEmployeeVo.getCompanyId());
                    return CommResult.error("该员工已在" + company.getName() + "任职");
                } else if (companyEmployee.getCompanyId().equals(companyEmployeeVo.getCompanyId())){
                    return CommResult.error("该员工已在本公司任职");
                }
            }
        }
        int affectCount = companyEmployeeService.update(companyEmployeeVo);
        if (affectCount <= 0){
            return CommResult.error();
        }
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("memberId",companyEmployeeVo.getMemberId());
        param.put("companyId",companyEmployeeVo.getCompanyId());
        param.put("enterpriseId", enterprise.getId());
        CompanyEmployeeVo result = companyEmployeeService.findById(param);
        return CommResult.success(result);
    }

    /**
     * 导入企业员工得信息
     *
     * @return
     */
    @ApiOperation("导入企业员工得信息")
    @PostMapping("/upload")
    public CommResult<CompanyEmployeeVo> upload(@RequestBody List<CompanyEmployeeVo> list, Long companyId){
        Enterprise enterprise = enterpriseService.getCurrent();
        if (null == enterprise) {
            return CommResult.error("没有开通企业");
        }

        /*Map<String,Object> map = companyEmployeeService.checkCompanyEmployee(list, enterprise.getMchId(), enterprise.getId(), companyId);
        if (map.get("returnCode").equals("success")) {
            companyEmployeeService.upload(list, enterprise.getMchId(), enterprise.getId(), companyId);
        }*/
        companyEmployeeService.cancelCompanyBinding(list, enterprise.getMchId(), enterprise.getId(), companyId);
        List<CompanyEmployeeVo> result = companyEmployeeService.upload(list, enterprise.getMchId(), enterprise.getId(), companyId);
        return CommResult.success(result);
    }

    /**
     * 统计某个企业的员工数量
     *
     * @return
     */
    @ApiOperation("统计某个企业的员工数量")
    @GetMapping("/countEmployeeNum/{companyId}")
    public CommResult<CompanyEmployee> countEmployeeNum(@PathVariable Long companyId) {
        Enterprise enterprise = enterpriseService.getCurrent();
        if (null == enterprise) {
            return CommResult.error("没有开通企业");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("enterpriseId", enterprise.getId());
        params.put("companyId", companyId);
        params.put("status", 1);
        int num = companyEmployeeService.countCompanyEmployeeNum(params);

        return CommResult.success(num);

    }

}
